package cn.sirun.com.fca.personal.center.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.PatternMatchingUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.uikit.utils.DialogUtils;
import com.tencent.cloud.uikit.utils.SystemUiUtils;
import com.tencent.cloud.uikit.widget.edittext.LineEditText;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.constant.AccountIntentConst;
import cn.sirun.com.fca.personal.center.presenter.IChangeRegisterAccountPresenter;
import cn.sirun.com.fca.personal.center.presenter.impl.ChangeRegisterAccountPresenter;
import cn.sirun.com.fca.personal.center.view.IIdentityNumberView;

public class IdentityNumberFlowView extends BaseLoadingFlowView<IChangeRegisterAccountPresenter>
        implements IIdentityNumberView {

    private TextView mPromptTV, mNextTV;
    private View mLineView;
    private ImageView mPromptIV;
    private LineEditText mNumberET;

    private String mIdentityNum;
    private int mFrom;

    public IdentityNumberFlowView(Activity activity) {
        super(activity);
        mFrom = getActivity().getIntent().getIntExtra(AccountIntentConst.FROM, AccountIntentConst.UNKNOWN);
    }

    public IdentityNumberFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();

    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        SystemUiUtils.showActionBar(activity, "");
    }

    @Override
    public void onSubmitIdentityNumResult(BaseResponse<Void> response) {
        hideLoadingView();
        if (StatusCode.SUCCESS == response.getCode()) {
            Intent intent = Router.fromPath(AccountActivityConst.Path.CONFIRM_PASSWORD);
            intent.putExtra(AccountIntentConst.FROM, mFrom);
            ActivityUtils.startActivityForResult(getActivity(), intent);
            return;
        }
        DialogUtils.showToast(getActivity(), response.getMsg());
    }

    @Override
    protected IChangeRegisterAccountPresenter createPresenter() {
        return new ChangeRegisterAccountPresenter();
    }

    private void initView() {
        setContentView(R.layout.identity_number_activity);
        final Activity activity = getActivity();
        mPromptTV = activity.findViewById(R.id.tv_number_prompt);
        mNextTV = activity.findViewById(R.id.tv_next);
        mLineView = activity.findViewById(R.id.v_number_line);
        mPromptIV = activity.findViewById(R.id.iv_number_prompt);
        mNumberET = activity.findViewById(R.id.et_number);

        mNumberET.setTextChangeListener(new LineEditText.OnTextChangeListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setConfirmBtnEnable();
            }
        });

        mNextTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkFormat()) {
                    showLoadingView();
                    getPresenter().submitIdentityNumber(mIdentityNum);
                }
            }
        });
    }

    private boolean checkFormat() {
        mIdentityNum = mNumberET.getText().toString().trim();
        boolean isNumberCorrect = PatternMatchingUtils.isIdentityNumber(mIdentityNum);
        if (isNumberCorrect) {
            return true;
        } else {
            setNumberBg(R.string.tv_identity_number_error, R.color.login_register_text_color,
                    R.color.login_register_text_color, View.VISIBLE);
            return false;
        }
    }

    private void setConfirmBtnEnable() {
        mIdentityNum = mNumberET.getText().toString();
        int numberLength = mNumberET.length();
        boolean isNumberCorrect = PatternMatchingUtils.isIdentityNumber(mIdentityNum);
        if (0 == numberLength || isNumberCorrect) {
            setNumberBg(R.string.dientity_number, R.color.grey_A9,
                    R.color.grey_DD, View.GONE);
        }

        boolean isEnable = (PatternMatchingUtils.IDENTITY_NUMBER_LENGTH_OF_15 == numberLength
                || PatternMatchingUtils.IDENTITY_NUMBER_LENGTH_OF_18 == numberLength);

        if (isEnable) {
            mNextTV.setBackgroundResource(R.drawable.s_btn_bg_border);
        } else {
            mNextTV.setBackgroundResource(R.drawable.s_btn_default_bg_border);
        }
        mNextTV.setEnabled(isEnable);
    }

    private void setNumberBg(int text, int textColor, int LineColor, int visibility) {
        mPromptTV.setText(ResourcesUtils.getString(text));
        mPromptTV.setTextColor(ResourcesUtils.getColor(textColor));
        mLineView.setBackgroundColor(ResourcesUtils.getColor(LineColor));
        mPromptIV.setVisibility(visibility);
    }

}
